package com.vgooo.client.myvg;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.vgooo.bean.UserInfo;
import com.vgooo.client.VGApplication;
import com.vgooo.util.HttpUtils;
import com.vgooo.util.JsonParserUtils;
import com.vgooo.util.MD5Utils;

public class OtherLogin {
	
	public static final int TYPE_QQ  = 1 ;
	public static final int TYPE_WEI_BO  = 2 ;
	
	private static final String LOGIN_URL = HttpUtils.BASE_URI +"otherlogin" ;
	private static final String BIND_URL = HttpUtils.BASE_URI +"bind" ;
	/**
	 * QQ、微博登录
	 * @param activity
	 * @param token
	 * @param type
	 * @param listener
	 */
	public static void OtherLogin(final Activity activity ,final String token,final int type, final LoginListener listener){
		if(null==activity||null==token||"".equals(token)){
			return ;
		}
	
		new AsyncTask<String, String, String>(){

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				String urlPath = LOGIN_URL +"?t="+type +"&token="+token ;
				Map<String,String> param = new HashMap<String, String>() ;
				param.put("t", String.valueOf(type));
				param.put("token", token);
				log("url : " + urlPath) ;
				String result = null ;
				try {
//					result = HttpUtils.getJsonString(urlPath);
					result = HttpUtils.doPost(LOGIN_URL, param).trim();
					if(null!=result&&!"1".equals(result)&&!"2".equals(result)){
						UserInfo userInfo = JsonParserUtils.getUserInfo(result);
						userInfo.isLogin = true ;
						userInfo.otherLogin = type ;
						switch (type) {
						case TYPE_QQ:
							userInfo.QQToken = token ;
							break;
						case TYPE_WEI_BO:
							userInfo.weiBoToken = token ;
							break;
						default:
							break;
						}
						VGApplication.userInfo = userInfo ;
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					if(null!=listener){
						listener.fail(e.toString(),null);
					}
					e.printStackTrace();
				}
				return result;
			}
			
			protected void onPostExecute(String result) {
				if(null==result||"".equals(result)){
					log(result +"、未知错误");
					if(null!=listener){
						listener.fail(result,token);
					}
					return ;
				}
				if("1".equals(result)){
					log(result +"、数据出错") ;
					if(null!=listener){
						listener.fail(result,token);
					}
					return ;
				}
				if("2".equals(result)){
					log(result +"、登录失败") ;
					if(null!=listener){
						listener.fail(result,token);
					}
					return ;
				}
				if(VGApplication.userInfo.isLogin){
					if(null!=listener){
						listener.success(result);
					}
					log("登录成功：" +result) ;
				}
				
			};
			
		}.execute("");
	}
	/**
	 * QQ、微博绑定账号
	 * @param activity
	 * @param userNmae
	 * @param pwd
	 * @param token
	 * @param type
	 * @param listener
	 */
	public static void BindLogin(final Activity activity,final String userNmae,final String pwd ,final String token,final int type ,final LoginListener listener){
		if(null==activity||null==userNmae||"".equals(userNmae)||null==pwd||"".equals(pwd)||null==token||"".equals(token)){
			return ;
		}
		
		new AsyncTask<String, String, String>(){

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				String code = MD5Utils.getMD5(userNmae+MD5Utils.getMD5(pwd+"vgooobind")) ;
//				String urlPath = BIND_URL +"?token=" +token+"&username="+userNmae+"&psd="+pwd+"&type="+type+"&code="+code ;
				Map<String,String> map = new HashMap<String, String>();
				map.put("token", token);
				map.put("username", userNmae);
				map.put("psd", pwd);
				map.put("type", String.valueOf(type));
				map.put("code", code);
//				log("url : " + urlPath) ;
				String result = null ;
				try {
//					result = HttpUtils.getJsonString(urlPath).trim();
					result = HttpUtils.doPost(BIND_URL, map).trim();
					log("BindLogin JsonString : "+result) ;
					if(null!=result&&!"1".equals(result)&&!"2".equals(result)){
						UserInfo userInfo = JsonParserUtils.getUserInfo(result);
						userInfo.isLogin = true ;
						userInfo.otherLogin = type ;
						switch (type) {
						case TYPE_QQ:
							userInfo.QQToken = token ;
							break;
						case TYPE_WEI_BO:
							userInfo.weiBoToken = token ;
							break;
						default:
							break;
						}
						VGApplication.userInfo = userInfo ;
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					if(null!=listener){
						listener.fail(e.toString(),null);
					}
					e.printStackTrace();
				}
				return result;
			}
			
			protected void onPostExecute(String result) {
				if(null==result||"".equals(result)){
					log(result +"、未知错误");
					return ;
				}
				if("1".equals(result)){
					log(result +"、数据出错");
					return ;
				}
				if("2".equals(result)){
					log(result +"、登录失败");
					return ;
				}
				log("登录成功："+ result) ;
				if(null!=listener){
					listener.success(result);
				}
				
			};
			
		}.execute("");
	}
	
	
//	登录成功：{"user_id":"222728","user_name":"805015788@qq.com","user_email":"805015788@qq.com"}
	
	public interface LoginListener{
		void success(String info) ;
		void fail(String info,String token) ;
	}
	
	private static void log(String info){
		Log.i("OtherLogin",info) ;
	}

}
